package org.example.server.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;

import static org.example.server.util.SpringUtils.randomInt;

@Controller
@ResponseBody
public class ImgUtils {

    @Value("${ImgUtils.random-lbs-64}")
    public String value64;

    @Value("${ImgUtils.random-lbs-63}")
    public String value63;

    @Value("${ImgUtils.random-lbs-62}")
    public String value62;

    @Value("${ImgUtils.random-lbs-61}")
    public String value61;

    @Value("${ImgUtils.random-lbs-60}")
    public String value60;


    @GetMapping("/img")
    public void hello(HttpServletRequest request, HttpServletResponse response){
        try {

            FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(new Font("微软雅黑", Font.PLAIN, 22));//设计字体

            BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_BGR);//设置宽高，图片类型

            Graphics g = image.getGraphics();

            g.setClip(0, 0, 800, 600);//设置绘画的区域,默认背景是黑色，也叫做获取画笔，指定画笔可画的范围

            g.setColor(Color.darkGray);//设置画笔颜色

            g.fillRect(0, 0, 800, 600);//画一个矩形块,颜色是白色，因为画笔颜色是白色

            g.setColor(Color.white);//设置画笔颜色

            g.setFont(new Font("微软雅黑", Font.PLAIN, 22));//设计字体样式

            //画字符串
//            g.drawString(value64,20,50);
//            g.drawString(value63,20,100);
//            g.drawString(value62,20,150);
//            g.drawString(value61,20,200);
//            g.drawString(value60,20,250);

            g.drawString(String.valueOf(randomInt()),20,50);

            g.dispose();

            ImageIO.write(image, "jpg",response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
